package com.service.weChatWifiLogin.service.impl;


import com.service.config.utils.ExcelUtil;
import com.service.config.utils.RedisUtil;
import com.service.weChatWifiLogin.dto.NameListDto;
import com.service.weChatWifiLogin.service.ExcelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: CloudCampusEcology
 * @description:
 * @author: rui
 * @create: 2020-11-12 11:10
 **/
@Slf4j
@Service
public class ExcelServiceImpl implements ExcelService {
    @Value("${spring.excel.filepath}")
    private String filepath; //邮件发送者
    @Autowired
    public StringRedisTemplate stringRedisTemplate;

    @Override
    public List<NameListDto> batchImport(MultipartFile file) throws Exception {


        /*
         * 判断文件名的合法性
         * 判断文件是2003版or2007版
         */
        String fileName = file.getOriginalFilename();
        String[] fullName = null;
        if (fileName != null) {
            fullName = fileName.split("\\.");
        }
        //log.info(fullName[1]);
        boolean isExcel2003 = true;
        if (fullName != null) {
            String suffix = fullName[1];
            if (!suffix.equals("xls") && !suffix.equals("xlsx")) {
                throw new Exception("上传不正确");
            }
            if (suffix.equals("xlsx")) {
                isExcel2003 = false;
            }
        }
        InputStream iStream = file.getInputStream(); //IO流
        Workbook workbook = null; //创建工作簿
        if (isExcel2003) {
            workbook = new HSSFWorkbook(iStream); //2003版
        } else {
            workbook = new XSSFWorkbook(iStream); //2007版
        }
        //将文件存储到本地
        String savePath = null;
        try {
            savePath = filepath + fileName;
            log.info(savePath);
            OutputStream os = new FileOutputStream(savePath); // 保存到当前路径savePath
            workbook.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            log.error("File saving error !");
            e.printStackTrace();
        }
        Workbook workbook2 = WorkbookFactory.create(file.getInputStream());
        Sheet sheet = workbook2.getSheetAt(0);
        ExcelUtil<NameListDto> util = new ExcelUtil<>(NameListDto.class);
        List<NameListDto> nameListDtoList = util.importExcel(sheet.getSheetName(), workbook2, 2);
        return nameListDtoList;
    }
}
